/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;
/**
 * 说明：
 * 角色权限分配
 * @Author：李智深
 * @Date：2012-05-04
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TRole;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.IDNameBean;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.service.TEmployeeService;
import com.ruipengkj.view.baseDataView.DataEmployeesPanel;

public class SystemManagePrivilegeEmpAddDialog  extends IDialog implements Runnable{
	
	private static final long serialVersionUID = 1L;
	private JButton addButton;//添加选择
	private JButton delButton;//删除选择
	private JButton serupButton;//确定
	private JList jlist;
	private DefaultListModel jlistModel;
	private DataEmployeesPanel dataEmployeesPanel;//员工查询页面
	private TEmployeeService tEmployeeService;//员工
	private TRole tRole;
	private Set<TEmployee> tEmployeeSet;
	public SystemManagePrivilegeEmpAddDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tEmployeeService=BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		this.setSize(900, 700);
		this.setLocationRelativeTo(null);
		this.init();
		this.initListener();
	}
	private void init() {
		dataEmployeesPanel=new DataEmployeesPanel();
		JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,dataEmployeesPanel, this.downPanel());
		jsp.setDividerLocation(450);
		jsp.setOpaque(false);
		JPanel down_jp=uiFactory.createFlowJPanel();
		serupButton=new JButton("确定(Ctrl+S)");
		down_jp.add(serupButton);
		
		JPanel main = new BorderJPanel();
		main.setLayout(new BorderLayout());
		main.add(jsp);
		main.add(down_jp,BorderLayout.SOUTH);
		main.setBackground(new Color(247, 252, 255));
		main.setCursor(Util.CURSOR_DEFAULT);
		this.add(main);
//		this.add(jsp);
//		this.add(down_jp,BorderLayout.SOUTH);
	}
	private JPanel downPanel(){
		JPanel downPanel=uiFactory.createBorderJPanel();
		JPanel jp=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		addButton=new JButton("添加选中");
		delButton=new JButton("删除");
		jp.add(addButton);
		jp.add(delButton);
		jlistModel=new DefaultListModel();
		jlist=new JList(jlistModel);
		downPanel.add(jp,BorderLayout.NORTH);
		downPanel.add(new JScrollPane(jlist));
		return downPanel;
	}
	
	private void initListener(){
		addButton.addActionListener(this);
		delButton.addActionListener(this);
		serupButton.addActionListener(this);
		serupButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==addButton){
			//添加选中
			int[] ints=dataEmployeesPanel.getTable().getSelectedRows();
			if(ints!=null&&ints.length!=0){
				for(int i=0;i<ints.length;i++){
					String id=(String)dataEmployeesPanel.getTable().getValueAt(ints[i],0 );
					String name=(String)dataEmployeesPanel.getTable().getValueAt(ints[i], 3);
					boolean flag=true;
					int size=jlistModel.getSize();
					for(int j=0;j<size;j++){
						IDNameBean bean=(IDNameBean)jlistModel.getElementAt(j);
						if(id.equals(bean.getStrId())){
							flag=false;
							break;
						}
					}
					if(flag){
						jlistModel.addElement(new IDNameBean(id,name));
					}
				}
			}
		}else if(e.getSource()==delButton){
			//删除
			int[] ins=jlist.getSelectedIndices();
			if(ins!=null&&ins.length!=0){
				for(int i=0;i<ins.length;i++){
					jlistModel.remove(ins[i]-i);
				}
			}
		}else if(e.getSource()==serupButton){
			//确定
			int size=jlistModel.getSize();
			if(size!=0){
				if(tEmployeeSet!=null&&tEmployeeSet.size()!=0){
					Iterator<TEmployee> it=tEmployeeSet.iterator();
					while(it.hasNext()){
						TEmployee employee=it.next();
						employee.setTRole(null);
						try {
							tEmployeeService.modifyService(employee);
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
				
				for(int i=0;i<size;i++){
					IDNameBean bean=(IDNameBean)jlistModel.getElementAt(i);
					try {
						TEmployee employee=tEmployeeService.getByIdService(bean.getStrId());
						if(employee!=null){
							employee.setTRole(tRole);
							tEmployeeService.modifyService(employee);
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				try {
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加角色分配信息");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, "操作成功");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "请先选择需要添加该权限的员工");
			}
		} else {
			super.actionPerformed(e);
		}
	}
	@Override
	public void run() {
		
	}
	
	public void showDialog(TRole tRole){
		this.tRole=tRole;
		this.tEmployeeSet=tRole.getTEmployees();
		if(tEmployeeSet!=null&&tEmployeeSet.size()!=0){
			Iterator<TEmployee> it=tEmployeeSet.iterator();
			while(it.hasNext()){
				TEmployee employee=it.next();
				jlistModel.addElement(new IDNameBean(employee.getEmployeeID(),employee.getName()));
			}
		}
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SystemManagePrivilegeEmpAddDialog(null, "权限分配", true).setVisible(true);
	}
}
